;
(function ($) {
    var contexts = null; //沟通内容
    var nextdate = null; //下次日期
    var nexttime = null; //下次时间.

    function init() {
        //var btn = $("<button id='inject_btn' class='btn btn-primary' type='button' >&nbsp;自动回访</button>");
        //btn.click(clickHandle);
        //$(".gray-bg").append(btn);
        initContextArray();
        setTimeout(() => {
            find_communication_form();
        }, 500);
    }

    init();

    function initContextArray() {
        // contexts = stringToArray(getValue('context') || '关机|未接通|未接');
        // nextdate = stringToArray(getValue('nextdate') || '3|4|5');
        // nexttime = getValue('nexttime') || '10:00';

        getValue({
            'context': '关机|未接通|未接'
        }, function (result) {
            contexts = stringToArray(result.context || '关机|未接通|未接');
        });

        getValue({
            'nextdate': '3|4|5'
        }, function (result) {
            nextdate = stringToArray(result.nextdate || '关机|未接通|未接');
        });

        getValue({
            'nexttime': '10:00'
        }, function (result) {
            nexttime = stringToArray(result.nexttime || '10:00');
        });

        // chrome.runtime.sendMessage('context',function(response){
        //     contexts = stringToArray(response || '关机|未接通|未接');
        // });

        // chrome.runtime.sendMessage('nextdate',function(response){
        //     nextdate = stringToArray(response || '3|4|5');
        // });

        // chrome.runtime.sendMessage('nexttime',function(response){
        //     nexttime = response || '10:00';
        // });
    }

    function stringToArray(str) {
        if (str == null) {
            return [];
        }
        var temp = str.split('|');
        return temp;
    }

    function getRandValue(arr) {
        var z = parseInt(Math.random() * arr.length, 10);
        return arr[z];
    }

    //批量点击按钮.
    function clickHandle() {

    }

    function find_communication_form() {
        var dom = $("#collapseOne");
        if (dom.length != 0) {
            //var flag = $("#collapseOne").attr("aria-expanded");
            //var $test = $("#communication_form .layui-form-select input");
            //console.log($test);
            var $test = $("#communicationWay");
            if ($test) {
                var text = $test.val();
                if (text == '') {
                    add_Value();
                    //return false;
                }
            }
        }
        setTimeout(() => {
            find_communication_form();
        }, 500);
    }

    function add_Value() {

        // 沟通方式
        $("#communicationWay").val("电话");
        var gtfs = $("#communication_form .layui-form-select").eq(0);
        gtfs.find('input').val('电话');
        gtfs.find('dl dd').eq(0).addClass('layui-this');

        //沟通内容
        $("#otor").val(getRandValue(contexts));

        //下次沟通
        // $("#nextCommunication").val("电话");
        $("select[name='nextCommunication']").val("电话");
        var xcgt = $("#communication_form .layui-form-select").eq(1);
        xcgt.find('input').val('电话');
        xcgt.find('dll dd').eq(0).addClass('layui-this');



        //下次时间

        var addDateNumber = parseInt(getRandValue(nextdate)) || 1;
        var sDate = new Date().dateAdd('d', addDateNumber);
        var strDate = sDate.Format('yyyy-MM-dd');
        $("#nextCommunicationTime").val(strDate + ' ' + nexttime);
        // layui.form.render('select');
    }

})(jQuery);